<?php
namespace app\index\controller;
use think\Controller;
use think\Config;
use  think\Request;
use think\Db;
use think\Cookie;
use think\Session;


class Index extends Controller
{
    public function index()
    {
        
	
		
        $this->redirect('Index/login', []);
        
    }
	 public function login()
    {

  
        $request = Request::instance();
		if($request->isPost()){
            $username =$request->post("username");
            
            $password =  md5($request->post("password"));
            $logincheck =$request->post("logincheck");



                $manageobj =db('manage')->where("username='".$username."' and password='".$password."'")->find();
 
                
                if($manageobj){
                    if($manageobj["state"]=="-1"){

                        $this->success('账号已禁用');
                    }
                   if($manageobj["issuper"]=="1"){

                        if($logincheck=="1"){
                            Cookie::set('islogin',1,['prefix'=>'think_','expire'=>3600]);
                            Cookie::set('issuper','是',['prefix'=>'think_','expire'=>3600]);
                        }
                        session("mid",$manageobj["mid"]);
                        session("userid",$manageobj["id"]);
                        session("issuper","1");
                        loginlog($manageobj["username"],"超级管理员",0,"pc");
                        $this->redirect('Home/index');
                   }else if($manageobj["isdaili"]=="1"){

                        if($logincheck=="1"){
                            Cookie::set('islogin',1,['prefix'=>'think_','expire'=>3600]);
                            Cookie::set('issuper','是',['prefix'=>'think_','expire'=>3600]);

                        }
                       
                        session("mid","-1");
                        session("userid",$manageobj["id"]);
                        session("dailiid",$manageobj["dailiid"]);
                        session("issuper","0");
                        loginlog($manageobj["username"],"代理商",0,"pc");

                        $this->redirect('Home/index');

                   }else{

                        $mechanism =db('mechanism')->where("id=".$manageobj["mid"])->find();
                        $endtime = $mechanism["endtime"];
                     //   echo $endtime;
                        if(strtotime($endtime)>time('Y-m-d')){
                         
                            if($logincheck=="1"){
                                Cookie::set('islogin',1,['prefix'=>'think_','expire'=>3600]);
                                Cookie::set('userid',$manageobj["id"],['prefix'=>'think_','expire'=>3600]);
    
                            }
                            session("userid",$manageobj["id"]);
                            session("username",$username);
                            session("mid",$manageobj["mid"]);
    
                            $mechanism = db("mechanism")->where("id=".$manageobj["mid"])->find();
                            loginlog($username,$mechanism["name"],$mechanism["id"],"pc");
                    
                            $this->redirect('Home/index');

                        }else{
                            $this->success('过期了，无法登陆');
                        }

                      
                   }
                   
              
                }else{
               
                     
                     $this->success('用户名密码错误');
                 
                    
                }


        }else{
       


            $islogin =  Cookie::get('islogin','think_');
            $issuper =  Cookie::get('issuper','think_');
            $userid =  Cookie::get('userid','think_');
            if($islogin=="1"){
                session("issuper",$issuper);
                session("userid",$userid);
                $this->redirect("Home/index"); 
            }else{
                return view();
            }
          
        }
	  
    }
    public function findpass(){
        if($_POST){
            $phone = $_POST["phone"];
            $code = $_POST["code"];

            $smsloginfo = db("smslog")->where("code='".$code."'")->find();
            if($smsloginfo){

                $minute=floor((time()- strtotime($smsloginfo["createtime"])) %86400/60);
                if($minute<=5){
                    $password = md5("123456");
                    $obj = Db::execute('update manage set password=? where username=?',[$password,$phone]);
                    if($obj==true){
                        return json(['status'=>200,'message'=>'重置密码成功，新的密码是123456']);
                    }else{
                        return json(['status'=>400,'message'=>'找回密码失败']);
                    }
        
                }else{
                    return json(['status'=>400,'message'=>'验证码超时了']);
                }
                

            }else{
                return json(['status'=>400,'message'=>'验证码不正确']);
                
            }


        }else{

            return view();
        }
       
    }
    public function exitlogin(){
        Cookie::clear('think_');
        Session::clear('think');

        $this->redirect("Index/login"); 
    }
   
}
